package com.rongji.dfish.hello.controller;

import com.rongji.dfish.base.Pagination;
import com.rongji.dfish.base.util.Utils;
import com.rongji.dfish.framework.mvc.controller.BaseActionController;
import com.rongji.dfish.framework.mvc.response.JsonResponse;
import com.rongji.dfish.framework.util.ServletUtil;
import com.rongji.dfish.hello.constant.DemoConstants;
import com.rongji.dfish.hello.entity.SysUser;
import com.rongji.dfish.hello.model.UserModel;
import com.rongji.dfish.hello.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class UserController extends BaseActionController {

    @Autowired
    private UserService userService;

    @Override
    protected int getPaginationLimit() {
        return 2;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request) {
        Pagination pagination = getPagination(request);

        Map<String, Object> params = new HashMap<>();
        params.put("orgId", request.getParameter("orgId"));
        params.put("userName", ServletUtil.getParameter(request, "userName"));
        params.put("loginName", ServletUtil.getParameter(request, "loginName"));
        params.put("gender", request.getParameter("gender"));

        List<UserModel> userList = userService.list(pagination, params);
        return new JsonResponse<>(userList).setPagination(pagination);
    }

    @RequestMapping("/get")
    @ResponseBody
    public Object get(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        SysUser user = null;
        if (Utils.notEmpty(userId)) {
            user = userService.get(userId);
        }
        if (user == null) {
            user = new SysUser();
            user.setOrgId(request.getParameter("orgId"));
            user.setGender("1");
            user.setUserStatus(DemoConstants.STATUS_ENABLE);
        }
        return new JsonResponse<>(user);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(HttpServletRequest request) throws Exception {
        UserModel user = new UserModel();
        bind(request, user);

        if (Utils.isEmpty(user.getUserId())) {
            user.setUserId(userService.getNewId());
            userService.save(user);
        } else {
            userService.update(user);
        }
        return new JsonResponse<>(user);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(HttpServletRequest request) throws Exception {
        String userId = request.getParameter("userId");
        for (String id : userId.split(",")) {
            userService.updateStatus(id, DemoConstants.STATUS_DELETE);
        }
        return new JsonResponse<>(userId);
    }

}
